﻿using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Security;
using DevExpress.ExpressApp.Security.Strategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SecurityDemo.Module
{
    public class SecurityDemoAuthentication : AuthenticationBase, IAuthenticationStandard
    {
        private SecurityDemoAuthenticationLogonParameters logonParameters;

        public SecurityDemoAuthentication()
        {
            logonParameters = new SecurityDemoAuthenticationLogonParameters();
        }
        public override void Logoff()
        {
            base.Logoff();
            logonParameters = new SecurityDemoAuthenticationLogonParameters();
        }
        public override object Authenticate(IObjectSpace objectSpace)
        {
            if (string.IsNullOrEmpty(logonParameters.UserName))
            {
                throw new UserFriendlyException("The 'User' field must not be empty.");
            }
            object user = objectSpace.FindObject<SecuritySystemUser>(new DevExpress.Data.Filtering.BinaryOperator("UserName", logonParameters.UserName));
            if (user == null)
            {
                throw new AuthenticationException(logonParameters.UserName);
            }
            return user;
        }
        public override IList<Type> GetBusinessClasses()
        {
            return new Type[] { typeof(SecurityDemoAuthenticationLogonParameters) };
        }
        public override bool AskLogonParametersViaUI
        {
            get { return true; }
        }
        public override object LogonParameters
        {
            get { return logonParameters; }
        }
        public override bool IsLogoffEnabled
        {
            get { return true; }
        }
    }

}
